package com.xiangyi.teachingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangyi.teachingsystem.dao.*;
import com.xiangyi.teachingsystem.entity.dto.CourseUserDto;
import com.xiangyi.teachingsystem.entity.po.*;
import com.xiangyi.teachingsystem.service.CourseService;
import com.xiangyi.teachingsystem.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    UserCourseMapper userCourseMapper;
    @Autowired
    CourseUserMapper courseUserMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    TaskService taskService;
    @Autowired
    CourseResMapper courseResMapper;

    @Override
    @Transactional
    public void addCourse(Course course) {
        Integer userId = course.getUserId();
        User user = userMapper.selectById(userId);
        String phoneNum = user.getPhoneNum();
        String sign = Long.toString(Long.parseLong(phoneNum), 36);
        System.out.println(sign);
        int i = new Random().nextInt(30);
        String substring = UUID.randomUUID().toString().replace("-", "").substring(i, i + 5);
        System.out.println(substring);
        course.setSign(sign.substring(0,5)+substring);
        courseMapper.insert(course);
        userCourseMapper.insert(new UserCourse(null,course.getUserId(),course.getId()));
    }

    @Override
    public List<Course> teachingCourseList(Integer userId) {
        return userCourseMapper.teachingCourseList(userId);
    }

    @Override
    public Course getOneByCourseIdAndUserId(String courseName, Integer userId) {
        return courseMapper.getOneByCourseIdAndUserId(courseName,userId);
    }

    @Override
    @Transactional
    public void delCourse(Integer courseId) {
        //删任务
        List<Task> taskList = taskService.getBaseMapper().selectList(new QueryWrapper<Task>().eq("course_id", courseId));
        for (Task task : taskList) {
            taskService.delTask(task.getId());
        }
        //删userCourse中间表
        userCourseMapper.delete(new QueryWrapper<UserCourse>()
                .eq("course_id",courseId));

        //删courseUser中间表
        Course courseInfo = courseMapper.selectById(courseId);
        courseUserMapper.delete(new QueryWrapper<CourseUser>()
                .eq("sign",courseInfo.getSign()));
        courseResMapper.delete(new QueryWrapper<CourseRes>()
                .eq("course_id",courseId));
        //删课程
        courseMapper.deleteById(courseId);
    }

    @Override
    public User getCourseUserBySign(String sign) {
        return courseUserMapper.getCourseUserBySign(sign);
    }

    @Override
    public CourseUserDto getInfoById(Integer courseId) {
        return courseMapper.getInfoById(courseId);
    }

    @Override
    public List<Course> courseList() {
        return courseMapper.courseList();
    }
}
